package ChaiPiao;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 菜花 on 2017/3/9.
 */
public class Game extends AbstractListEnumerable<String, PlayTypeValuesEnumeration> {

    /*****************************
     * fields
     ***********************************/

    private Set<GameSelection> selectionSet;
    /*****************************methods***********************************/
    //dan
    private boolean setDan = false;
    //gameId
    private String gameId;


    public Game(String gameId, boolean setDan) {
        this.setDan = setDan;
        this.gameId = gameId;
        selectionSet = new HashSet<>();
    }

    /**
     * 同样的选项只有一个
     *
     * @param selection
     */
    public void addSelection(GameSelection selection) {
        if (!typeContained(selection.getPlayType()))
            super.addSource(selection.getPlayType());
        selection.setGameId(this.getGameId());
        this.selectionSet.add(selection);
    }

    private boolean typeContained(String type) {
        for (GameSelection selection : this.selectionSet) {
            if (selection.getPlayType().equals(type))
                return true;
        }
        return false;
    }

    /*****************************
     * get set equals
     ***********************************/

    public boolean isSetDan() {
        return setDan;
    }

    public void setSetDan(boolean setDan) {
        this.setDan = setDan;
    }

    public String getGameId() {
        return gameId;
    }

    public void setGameId(String gameId) {
        this.gameId = gameId;
    }

    /**
     * game equal if it has the same race id
     *
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Game game = (Game) o;

        return gameId != null ? gameId.equals(game.gameId) : game.gameId == null;
    }


    /*****************************
     * override
     ***********************************/
    @Override
    protected PlayTypeValuesEnumeration source2result(String source) {
        PlayTypeValuesEnumeration playTypeValuesEnumeration = new PlayTypeValuesEnumeration(this.getGameId(), source);
        for (GameSelection selection : this.selectionSet) {
            if (selection.getPlayType().equals(source)) {
                playTypeValuesEnumeration.addTypeValue(selection.getTypeValue());
            }
        }
        return playTypeValuesEnumeration;
    }

}
